package com.lagou.service;

import com.lagou.dao.CodeDao;
import com.lagou.pojo.LagouAuthCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CodeService {

    @Autowired
    private CodeDao codeDao;

    public int addCode(String email,String code){
        LagouAuthCode lagouAuthCode = new LagouAuthCode();
        lagouAuthCode.setCode(code);
        lagouAuthCode.setEmail(email);
        Long currentTime = System.currentTimeMillis();
        lagouAuthCode.setCreatetime(new Date(currentTime));
        lagouAuthCode.setExpiretime(new Date(currentTime+1000*600));
        int id = codeDao.addCode(lagouAuthCode);
        return id;
    }

    public int selectCode(String email, String code){
        LagouAuthCode lagouAuthCode = codeDao.selectCode(email, code);
        if(lagouAuthCode == null){
            return 1;
        }
        Date expiretime = lagouAuthCode.getExpiretime();
        long expietimelong = expiretime.getTime();
        long currentTime = System.currentTimeMillis();
        if((currentTime - expietimelong) > 1000*600){
            return 2;
        }
        return 0;
    }
}
